package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.AppActSelfCarManagerBinding;
import com.sgb.kjwl.view.ui.fragment.transport.SelfCarManagerFragment;
import com.sgb.kjwl.viewmodel.transport.SelfCarManagerViewModel;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.view.adapter.CommPagerAdapter;

import java.util.ArrayList;

/**
 * @author: 杨畔
 * @date: 2021/10/9 09:59
 * @Description: 自营车辆管理页
 */
public class SelfCarManagerActivity extends BaseActivity<SelfCarManagerViewModel, AppActSelfCarManagerBinding> {
    private final String[] mTabs = {"全部", "审核通过", "审核不通过", "审核中", "已下架"};
    //
    private final ArrayList<Fragment> mFragments = new ArrayList<>();

    public static void start(Context context) {
        Intent intent = new Intent(context, SelfCarManagerActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    @Override
    protected int layoutId() {
        return R.layout.app_act_self_car_manager;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(SelfCarManagerViewModel.class);
    }

    @Override
    protected void init() {
        mBinding.includeTitle.titleView.setText("自营车辆");
        mBinding.includeTitle.rightView.setText("+自营车辆");
        initTab();
        initFragment();
        initEvent();
    }

    private void initTab() {
        for (String mTab : mTabs) {
            mBinding.tlSelfCarManager.addTab(mBinding.tlSelfCarManager.newTab().setText(mTab));
        }
    }

    private void initFragment() {
        for (int i = 0; i < mTabs.length; i++) {
            mFragments.add(SelfCarManagerFragment.newInstance(getState(i)));
        }
        CommPagerAdapter fragmentAdapter = new CommPagerAdapter(getSupportFragmentManager(), mFragments, mTabs);
        mBinding.vpSelfCarManager.setAdapter(fragmentAdapter);
        mBinding.vpSelfCarManager.setOffscreenPageLimit(4);
        //关联tabLayout与viewPager
        mBinding.tlSelfCarManager.setupWithViewPager(mBinding.vpSelfCarManager);
    }

    private void initEvent() {
        mBinding.includeTitle.ivBack.setOnClickListener(this);
        mBinding.includeTitle.rightView.setOnClickListener(this);
    }

    private int getState(int index) {
        //当前认证状态:0 审核中 1 审核通过 2 审核不通过 3 已下架
        int state = -1;
        switch (index) {
            case 0:
                state = -1;
                break;
            case 1:
                state = 1;
                break;
            case 2:
                state = 2;
                break;
            case 3:
                state = 0;
                break;
            case 4:
                state = 3;
                break;
        }
        return state;
    }

    @Override
    public void onMultiClick(View v) {
        if (v == mBinding.includeTitle.ivBack) {
            finish();
        } else if (v == mBinding.includeTitle.rightView) {
            AddSelfCarActivity.start(this);
        }
    }
}
